import {reqspecslist,reqspecscount} from "../../request/api"
const state = {
    list: [],
    size:2,
    count:0,
    page:1
}
const getters = {
    list(state) {
        return state.list
    },
    count(state){
        return state.count
    },
    size(state){
        return state.size
    }
}
const mutations = {
    changelist(state, list) {
        state.list = list
    },
    changecount(state,count){
        state.count=count
    },
    changepage(state,page){
        state.page=page
    }
}
const actions = {
    reqlist(context,isall=false) {
        let params=isall?{}:{
            page:context.state.page,
            size:context.state.size
        }
        reqspecslist(params).then(res => {
            if (res.data.code == 200) {
                let list = res.data.list ? res.data.list : []
                
                if(list.length==0&&context.state.page!=1){
                    context.commit('changepage',context.state.page-1)
                    context.dispatch('reqlist')
                    return
                }
                context.commit("changelist", list)
            }
        })
    },
    reqcount({commit}){
        reqspecscount().then(res=>{
            if (res.data.code == 200) {
                commit("changecount", res.data.list[0].total)
            }
        })
    },
    changePage(context,page){
        context.commit('changepage',page)
        context.dispatch('reqlist')
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}